package leetcode;

public class Q2460 {
    public int[] applyOperations(int[] nums) {
        for (int i = 0; i < nums.length - 1; ++i) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *= 2;
                nums[i + 1] = 0;
            }
        }

        for (int i = nums.length - 1; i >= 0; --i) {
            if (nums[i] == 0) {
                for (int j = i; j < nums.length - 1; ++j) {
                    nums[j] = nums[j + 1];
                }
                nums[nums.length - 1] = 0;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        Q2460 sl = new Q2460();
        int[] nums = {847, 847, 0, 0, 0, 399, 416, 416, 879, 879, 206, 206, 206, 272};
        int[] res = sl.applyOperations(nums);
        for (int i : res) {
            System.out.print(i + " ");
        }
    }
}